<template>
  <s-layout id="layout" :title="'公告'" navbar="normal" leftIconColor="#333" :autoBack="false" :bgColor="state.bgColor"
    :tabbarshow='false' :titleStyle="{ color: '#333', fontSize: '32rpx' }">
    <div class="call_on list_box" :style="{ paddingTop: (safeAreaInsets?.top + 44) + 'px' }">
      <scroll-view scroll-y class="comprehensive" v-if="lists.length" @scrolltolower='listScrolltolower'
        show-scrollbar="true" :refresher-enabled="isOpenRefresh" :refresher-triggered="triggered"
        :refresher-threshold="100" refresher-background="white" @refresherpulling="onPulling"
        @refresherrefresh="onRefresh" @refresherrestore="onRestore" @refresherabort="onAbort" @scroll="onScroll">
        <view class="item_wrap">
          <view class="item" v-for="(item, index) in lists" :key="index">
            <view class="item_left">
              <image :src="`${baseUrl}/uploads/uniapp_image/staff/Notification_icon1.png`" mode=""></image>
              <u-badge v-if="item.read ? false : true" :isDot="true" type="success" bgColor='#FF1212'
                class="badge"></u-badge>
            </view>
            <view class="item_right">
              <view class="title">{{ item.title }}</view>
              <view class="time">{{ formatDatetime(item.createtime) }}</view>
              <view class="look" @tap="lookDetails(item.id)">
                <view class="name">查看详情</view>
                <u-icon size="12" name="arrow-right" color="#999999"></u-icon>
              </view>
            </view>
            <view :class="item.read ? 'read' : 'status'"> {{ item.read ? '已读' : '未读' }}</view>

          </view>

        </view>
        <!-- 加载中 && 没有更多数据了 -->
        <view>
          <s-loadmore-gather :current_page="listPage" :last_page="listlast_page" ref="loadmoreRef"></s-loadmore-gather>
        </view>
        <!-- <view class="loading-text">
					{{ listPage.value < listlast_page.value ? '正在加载...' : '没有更多了~'  }}
				</view> -->
      </scroll-view>
      <u-empty v-else mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" text="暂无内容"
        marginTop="50%"></u-empty>
    </div>

  </s-layout>
</template>

<script setup>
import {
  formatDatetime
} from '@/utils/fn.js';
import { baseUrl } from '@/sheep/config';
import sheep from '@/sheep';
import { onLoad, onReachBottom, onShow } from "@dcloudio/uni-app";
import { ref } from 'vue';
const { safeAreaInsets } = uni.getSystemInfoSync()
const userStoreValue = uni.getStorageSync('user-store');
const userStoreObject = JSON.parse(userStoreValue)
const state = ref({
  bgColor: '#fff'
})
onLoad((options) => {
  
})

onShow(() => {
  lists.value = []
  getNotificationList()
}) 

const lists = ref([])
//数据请求
const listPage = ref()
const listlast_page = ref();
const listpagenumber = ref(1);

const getNotificationList = async () => {
  let params = {
    filter: JSON.stringify({
      department_id:userStoreObject.userInfo.department.deparment.id
    }),
    op: JSON.stringify({
      department_id:'='
    })
  };
  const res = await sheep.$api.notification.notificationProjectIndex(params);
  lists.value = [...lists.value, ...res?.data.rows];
  console.log(lists.value,'数组数据');

  listPage.value = res.data.current_page
  listlast_page.value = res.data.last_page

}
//滚动触底
const listScrolltolower = () => {
  loadmoreRef.value.PopupScrolltolower()
  if (listPage.value < listlast_page.value) {
    listpagenumber.value += 1;
  } else {
    return;
  }
  getNotificationList();
};

//  //查看详情
const lookDetails = (e) => {
  sheep.$router.go('/subpackage/user/user/user_notification/user_notification_detail', {
    id: e,
  });
}
// 页面下拉刷新
const freshing = ref(false)
const triggered = ref(false)
const isOpenRefresh = ref(true)  // 是否开启下拉
// 自定义下拉刷新控件被下拉
const onPulling = (e) => {
  if (e.detail.deltaY < 0) return  // 防止上滑页面也触发下拉
  triggered.value = true;
}
// 自定义下拉刷新被触发
const onRefresh = () => {
  if (freshing.value) return;
  freshing.value = true;
  setTimeout(() => {
    listpagenumber.value = 1;
    lists.value = []
    triggered.value = false;
    freshing.value = false;
    getNotificationList();
  }, 500);
}
// 自定义下拉刷新被复位
const onRestore = () => {
  triggered.value = 'restore'; // 需要重置
  // console.log("onRestore");
}
// 自定义下拉刷新被中止
const onAbort = () => {
  // console.log("onAbort");
}
</script>

<style lang="scss" scoped>
// 滚动
.comprehensive {
  width: calc(100% - 40rpx);
  background-color: rgba(0, 0, 0, 0);
  margin: 0 20rpx;
  height: 88vh;
}

//列表
.call_on {
  width: 100%;
  height: 89vh;

  .list_box {
    width: 100%;
    height: auto;
  }

}

.loading-text {
  text-align: center;
  font-size: $uni-font-size-base;
  color: #666;
  padding: 20rpx 0 50rpx;
}

.item_wrap {
  margin-top: 20rpx;

  .item {
    margin-bottom: 20rpx;
    border-radius: 16rpx;
    background-color: #FFFFFF;
    box-shadow: 0 4rpx 8rpx 0 rgba(0, 0, 0, 0.05);
    display: flex;
    position: relative;

    .item_left {
      margin: 30rpx 20rpx 30rpx 30rpx;
      position: relative;

      image {
        width: 68rpx;
        height: 68rpx;
      }

      :deep(.u-badge) {
        position: absolute;
        right: 2rpx;
        top: 2rpx;
        z-index: 9;
      }
    }

    .item_right {
      margin: 30rpx 0 10rpx 0;
      width: 80%;
      padding-right: 30rpx;
      font-family: PingFangSC;
      color: $uni-color-primary;
      font-weight: 400;
      font-size: 22rpx;
      display: flex;
      flex-direction: column;

      .title {
        font-family: PingFangSC;
        color: $uni-text-color;
        font-weight: 500;
        font-size: $uni-font-size-base;
        // height: 86rpx;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        text-overflow: ellipsis;
        margin-bottom: 16rpx;
        text-overflow: ellipsis;
        overflow: hidden;
      }

      .time {
        font-family: PingFangSC;
        color: #999999;
        font-weight: 400;
        font-size: $uni-font-size-sm;
        margin-bottom: 16rpx;
      }

      .look {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-top: 1rpx solid #efefef;
        height: 80rpx;
        line-height: 80rpx;
        padding-left: 20rpx;

        .name {
          font-family: PingFangSC;
          color: #666666;
          font-weight: 400;
          font-size: $uni-font-size-sm;

        }
      }
    }

    .status {
      font-family: PingFangSC;
      color: $uni-color-primary;
      font-weight: 500;
      font-size: 22rpx;
      position: absolute;
      right: 30rpx;
      top: 35rpx;
      z-index: 3;

    }

    .read {
      font-family: PingFangSC;
      color: #999;
      font-weight: 500;
      font-size: 22rpx;
      position: absolute;
      right: 30rpx;
      top: 35rpx;
      z-index: 3;
    }
  }
}
</style>